package com.cqupt.controller;


import com.cqupt.common.log.annotation.OperationLog;
import com.cqupt.pojo.ResponseMessage;
import com.cqupt.service.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Controller
@RequestMapping("business")
@ResponseBody
public class BusinessController {
    @Autowired
    private BusinessService businessService;


    /**
     * 获得MySQL的运行状态
     *
     * @param params 根据hostid查询
     *               返回结果 1为up 0为down
     * @return
     */
    @RequestMapping("/get_mysql_status")
    public ResponseMessage getMysqlStatus(@RequestBody Map<String, Object> params) {
        ResponseMessage responseMessage = new ResponseMessage();
        String hostid = params.get("hostid").toString();
        Integer flag = businessService.getMysqlStatus(hostid);
        Map<String, Object> result = new HashMap<>();
        result.put("result", flag);
        responseMessage.setStatus(true);
        responseMessage.setContent(result);
        return responseMessage;
    }

    /**
     * 获取所有的业务（不包括其下的设备）
     * 前端无需传参
     * @return 所有的业务列表
     */
    @RequestMapping("/get_all_business")
    public ResponseMessage getAllBusiness() {
        ResponseMessage responseMessage = new ResponseMessage();
        List<Map<String, Object>> allBusiness = businessService.getAllBusiness();
        Map<String, Object> result = new HashMap<>();
        result.put("result", allBusiness);
        responseMessage.setContent(result);
        responseMessage.setStatus(true);
        return responseMessage;
    }

    /**
     * 获取业务下的 主机
     *  前端传入业务的 id
     * @param params 包含 业务id 的map集合
     * @return 该业务下的所有主机
     */
    @RequestMapping("/get_business_info_host_by_id")
    public ResponseMessage getBusinessInfoHostById(@RequestBody Map<String, Object> params) {
        ResponseMessage responseMessage = new ResponseMessage();
        List<Map<String, Object>> device = businessService.getBusinessInfoDeviceById(params);
        Map<String, Object> result = new HashMap<>();
        result.put("result", device);
        responseMessage.setContent(result);
        responseMessage.setStatus(true);
        return responseMessage;
    }

    /**
     * 获取业务下的 数据库
     * 前端传入业务的 id
     * @param params 包含 业务id 的map集合
     * @return 该业务下的所有数据库
     */
    @RequestMapping("/get_business_info_database_by_id")
    public ResponseMessage getBusinessInfoDatabaseById(@RequestBody Map<String, Object> params) {
        ResponseMessage responseMessage = new ResponseMessage();
        List<Map<String, Object>> database = businessService.getBusinessInfoDatabaseById(params);
        Map<String, Object> result = new HashMap<>();
        result.put("result", database);
        responseMessage.setContent(result);
        responseMessage.setStatus(true);
        return responseMessage;
    }

    /**
     * 获取业务的下的 主机、数据库
     * 前端传入业务的 id
     * @param params 包含 业务id 的map集合
     * @return 该业务下的所有数据库和主机
     */
    @RequestMapping("/get_business_info")
    public ResponseMessage getBusinessInfo(@RequestBody Map<String, Object> params) {
        ResponseMessage responseMessage = new ResponseMessage();
        List<Map<String, Object>> info = businessService.getBusinessInfoById(params);
        Map<String, Object> result = new HashMap<>();
        result.put("result", info);
        responseMessage.setContent(result);
        responseMessage.setStatus(true);
        return responseMessage;
    }

    /**
     * 获取所有的主机（sql）
     * 无需传参
     * @return 所有的主机列表（通过查数据库）
     */
    @RequestMapping("/get_all_hosts")
    public ResponseMessage getAllHosts() {
        ResponseMessage responseMessage = new ResponseMessage();
        List<Map<String, Object>> hosts = businessService.getAllHosts();
        Map<String, Object> result = new HashMap<>();
        result.put("result", hosts);
        responseMessage.setContent(result);
        responseMessage.setStatus(true);
        return responseMessage;
    }

    /**
     * 获取所有的数据库
     * 无需传参
     * @return 所有的数据库列表
     */
    @RequestMapping("/get_all_databases")
    public ResponseMessage getAllDatabases() {
        ResponseMessage responseMessage = new ResponseMessage();
        List<Map<String, Object>> databases = businessService.getAllDatabases();
        Map<String, Object> result = new HashMap<>();
        result.put("result", databases);
        responseMessage.setContent(result);
        responseMessage.setStatus(true);
        return responseMessage;
    }

    /**
     * 获得全部数据库信息 （用于数据库管理）
     * @param pageSize
     * @param currentPage
     * @return
     */
    @RequestMapping("/get_all_databases_real")
    public ResponseMessage getAlldatabases(@RequestParam("pageSize") String pageSize, @RequestParam("currentPage") String currentPage) {
        ResponseMessage responseMessage = new ResponseMessage();
        List<Map<String, Object>> resultList = businessService.getAllDatabasesReal(pageSize, currentPage);
        Map<String, Object> result = new HashMap<>();
        result.put("result", resultList);
        responseMessage.setStatus(true);
        responseMessage.setContent(result);
        return responseMessage;
    }


    /**
     * 获得全部数据库数量（计数）
     * @return
     */
    @RequestMapping("/get_all_databases_counts")
    public ResponseMessage getAllDatabasesCount() {
        ResponseMessage responseMessage = new ResponseMessage();
        Integer resultList = businessService.getAllDatabasesCount();
        Map<String, Object> result = new HashMap<>();
        result.put("result", resultList);
        responseMessage.setStatus(true);
        responseMessage.setContent(result);
        return responseMessage;
    }

    /**
     * 添加数据库
     * @param params  Databases实体类
     * @return
     */
    @OperationLog(level = "信息", object = "数据库", operationType = "新增")
    @RequestMapping("/add_a_databases")
    public ResponseMessage addAdatabases(@RequestBody Map<String, Object> params) {
        ResponseMessage responseMessage = new ResponseMessage();
        Map<String, Object> resMap = new HashMap<>();
        Integer resultList = businessService.addDatabases(params);
        resMap.put("log_objectName",params.get("databasestype")+"数据库"+"---"+params.get("databasesip"));
        if (resultList == 1){
            resMap.put("log_description","");
            responseMessage.setStatus(true);
        }else{
            responseMessage.setErrorCode("已存在"+params.get("databasestype")+"数据库"+"---"+params.get("databasesip")+"!");
        }
        resMap.put("result",resultList);
        responseMessage.setContent(resMap);
        return responseMessage;
    }


    /**
     * 删除数据库
     * @param databasesid 根据databasesid删除
     * @return
     */
    @OperationLog(level = "信息", object = "数据库", operationType = "删除")
    @RequestMapping("/delete_a_databases")
    public ResponseMessage delAdatabases(@RequestParam(value = "databasesid") String databasesid) {
        ResponseMessage responseMessage = new ResponseMessage();
        Map<String, Object> result = new HashMap<>();

        Map<String, Object> databasesList = businessService.getADatabases(databasesid);
        Integer resultList = businessService.delDatabases(databasesid);
        result.put("result", resultList);
        result.put("log_objectName",databasesList.get("databasestype")+"数据库"+"---"+databasesList.get("databasesip"));
        if (resultList == 1){
            result.put("log_description","");
            responseMessage.setStatus(true);
        }else{
            responseMessage.setErrorCode("已存在"+databasesList.get("databasestype")+"数据库"+"---"+databasesList.get("databasesip")+"!");
        }
        responseMessage.setContent(result);
        return responseMessage;
    }

    /**
     * 根据databasesid获得数据库 （用于修改数据库）
     * @param databasesid
     * @return
     */
    @RequestMapping("/get_a_databases")
    public ResponseMessage getAdatabases(@RequestParam(value = "databasesid") String databasesid) {
        ResponseMessage responseMessage = new ResponseMessage();
        Map<String, Object> resultList = businessService.getADatabases(databasesid);
        Map<String, Object> result = new HashMap<>();
        result.put("result", resultList);
        responseMessage.setStatus(true);
        responseMessage.setContent(result);
        return responseMessage;
    }

    /**
     * 修改主机
     * @param params Databases实体类
     * @return
     */
    @OperationLog(level = "信息", object = "数据库", operationType = "修改")
    @RequestMapping("/modify_a_databases")
    public ResponseMessage modifyAdatabases(@RequestBody Map<String, Object> params) {
        ResponseMessage responseMessage = new ResponseMessage();
        Map<String, Object> result = new HashMap<>();
        Integer resultList = businessService.modifyDatabases(params);
        result.put("result", resultList);
        result.put("log_objectName",params.get("databasestype")+"数据库"+"---"+params.get("databasesip"));
        if (resultList == 1){
            result.put("log_description","");
            responseMessage.setStatus(true);
        }else{
            responseMessage.setErrorCode("已存在"+params.get("databasestype")+"数据库"+"---"+params.get("databasesip")+"!");
        }
        responseMessage.setContent(result);
        return responseMessage;
    }


    /**
     * 删除业务（取消主机、数据库关联）
     * 前端传参
     * @param params 包括 name、type、id 等属性的map结合
     * @return 是否删除成功
     */
    @OperationLog(level = "信息", object = "业务", operationType = "删除")
    @RequestMapping("delete_business_or_relation")
    public ResponseMessage deleteBusinessById(@RequestBody Map<String, Object> params) {
        ResponseMessage responseMessage = new ResponseMessage();
        Map<String, Object> result = new HashMap<>();
        //  获取参数项
        Map<String, Object> item = (Map<String, Object>) params.get("tableItem");
        String name = (String) item.get("name");
        String type = (String) item.get("type");
        Integer i = -1;
        // 根据类型来删除 某业务下的 数据库和主机
        if ("业务".equals(type)) {
            result.put("log_objectName", name);
            i = businessService.deleteBusinessById(item);
            result.put("log_description", "删除业务[" + name + "]");
        } else if ("设备".equals(type)) {
            i = businessService.cancelBusinessAndHostRelation(item);
            String business_name = businessService.getBusinessNameById((Integer) item.get("business_id"));
            result.put("log_objectName", business_name);
            result.put("log_description", "修改业务[" + business_name + "] 删除设备信息[" + name + "]" );
        } else if ("数据库".equals(type)) {
            i = businessService.cancelBusinessAndDatabasesRelation(item);
            String business_name = businessService.getBusinessNameById((Integer) item.get("business_id"));
            result.put("log_objectName", business_name);
            result.put("log_description", "修改业务[" + business_name + "] 删除数据库[" + name + "]" );
        }
        result.put("result", i);
        responseMessage.setContent(result);
        responseMessage.setStatus(true);
        return responseMessage;
    }


//    /**
//     * 删除 业务 和 主机 的关系
//     *
//     * @param params
//     * @return
//     */
//    @RequestMapping("/cancel_business_host_relation")
//    public ResponseMessage cancelBusinessAndHostRelation(@RequestBody Map<String, Object> params) {
//        ResponseMessage responseMessage = new ResponseMessage();
//        Integer i = businessService.cancelBusinessAndHostRelation(params);
//        Map<String, Object> result = new HashMap<>();
//        result.put("result", i);
//        responseMessage.setContent(result);
//        responseMessage.setStatus(true);
//        return responseMessage;
//    }
//
//    /**
//     * 删除 业务 和 数据库的关系
//     *
//     * @param params
//     * @return
//     */
//    @RequestMapping("/cancel_business_databases_relation")
//    public ResponseMessage cancelBusinessAndDatabasesRelation(@RequestBody Map<String, Object> params) {
//        ResponseMessage responseMessage = new ResponseMessage();
//        Integer i = businessService.cancelBusinessAndDatabasesRelation(params);
//        Map<String, Object> result = new HashMap<>();
//        result.put("result", i);
//        responseMessage.setContent(result);
//        responseMessage.setStatus(true);
//        return responseMessage;
//    }

    /**
     * 添加业务 同时添加业务与主机、数据库的关系
     * 前端传入参数
     * @param params 包括业务名称、选择的主句id 、数据库id
     * @return
     */
    @OperationLog(level = "信息", object = "业务", operationType = "新增")
    @RequestMapping("/add_business")
    public ResponseMessage addBusiness(@RequestBody Map<String, Object> params) {
        ResponseMessage responseMessage = new ResponseMessage();
        String name = (String) params.get("name");
        Integer i = businessService.addBusiness(params);
        Map<String, Object> result = new HashMap<>();
        // 此处为将操作记录到审计日志
        if (i >= 1) {
            responseMessage.setStatus(true);
            result.put("log_objectName", name);
            result.put("log_description", "新增业务[" + name+ "]");
        }
        result.put("result", i);
        responseMessage.setContent(result);
        return responseMessage;
    }

    /**
     * 添加业务和主机的关系
     *
     * @param params 包含多个 主机id 的集合、业务id
     * @return
     */
    @OperationLog(level = "信息", object = "业务", operationType = "修改")
    @RequestMapping("add_business_host_relation")
    public ResponseMessage addBusinessAndHostRelation(@RequestBody Map<String, Object> params) {
        ResponseMessage responseMessage = new ResponseMessage();
        Integer i = businessService.addBusinessAndHostRelation(params);
        Map<String, Object> result = new HashMap<>();
        // 此处为了将操作记录到 审计日志中
        String business_name = businessService.getBusinessNameById((Integer) params.get("id"));
        List<String> names = businessService.getHostNameByIds((List<Integer>) params.get("hostList"));
        if (i >= 1 ){
            result.put("result", i);
            result.put("log_objectName", business_name);
            result.put("log_description","修改业务[" + business_name + "] 新增主机" + names);
            responseMessage.setStatus(true);
        }
        responseMessage.setContent(result);
        return responseMessage;
    }

    /**
     * 添加业务和数据库的关系
     *
     * @param params 包含多个数据库id 的集合、业务id
     * @return
     */
    @OperationLog(level = "信息", object = "业务", operationType = "修改")
    @RequestMapping("/add_business_database_relation")
    public ResponseMessage addBusinessAndDatabaseRelation(@RequestBody Map<String, Object> params) {
        ResponseMessage responseMessage = new ResponseMessage();
        Integer i = businessService.addBusinessAndDatabaseRelation(params);
        Map<String, Object> result = new HashMap<>();
        // 以下为了将操作记录到日志中
        String business_name = businessService.getBusinessNameById((Integer) params.get("id"));
        List<String> names = businessService.getDatabaseNameByIds((List<Integer>) params.get("databaseList"));
        if (i >= 1 ){
            result.put("result", i);
            result.put("log_objectName", business_name);
            result.put("log_description","修改业务[" + business_name + "] 新增数据库" + names);
            responseMessage.setStatus(true);
        }
        responseMessage.setContent(result);
        return responseMessage;
    }

    /**
     * 根据主机或数据库id获取业务
     * @param type 设备或数据库
     * @param id  主机id或数据库id
     * @return
     */
    @RequestMapping("get_business_by_id")
    public ResponseMessage getBusinessById(@RequestParam(value = "type") String type,@RequestParam(value = "id") String id) {
        ResponseMessage responseMessage = new ResponseMessage();
        List<Map<String, Object>> resultList;
        if (type.equals("数据库")){
            resultList = businessService.getBusinessByDatabaseId(id);
        }
        else {
            resultList = businessService.getBusinessByHostId(id);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("result",resultList);
        responseMessage.setStatus(true);
        responseMessage.setContent(result);
        return responseMessage;
    }

    /**
     * 根据主机id获取业务
     * @param hostId
     * @return
     */
    @RequestMapping("get_business_by_hostId")
    public ResponseMessage getBusinessByHostId(@RequestParam(value = "hostId") String hostId) {
        ResponseMessage responseMessage = new ResponseMessage();
        List<Map<String, Object>> resultList = businessService.getBusinessByHostId(hostId);
        Map<String, Object> result = new HashMap<>();
        result.put("result",resultList);
        responseMessage.setStatus(true);
        responseMessage.setContent(result);
        return responseMessage;
    }
}
